//index.js
//获取应用实例
const constans = require('../../constans/constans.js');
const request = require('../../request.js');
const Toast = require('../../utils/Toast.js');
const LogUtils = require('../../utils/LogUtils.js');
const util = require('../../utils/util.js')
const app = getApp();

Page({
  data: {
      yanzhengma:'获取验证码',
    yanzhengma_bool: true,
    phoneT:'',
    height1:'',
    optBind:0,
    optBind_bg: 0,
    dipBind:'none',
    phoneBool: 'none',
    phoneBool1:'none',
    phOne:'',
    optBinds:'.6',
  },
  //事件处理函数

  onLoad: function () {
    app.requestHeader = { 'Authorization': wx.getStorageSync('Authorization')};
    let that = this;

    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        // 获取可使用窗口宽度
        let clientHeight = res.windowHeight;
        // 获取可使用窗口高度
        let clientWidth = res.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = clientHeight * ratio;
        // 设置高度
        that.setData({
          height1: height
        });
      }
      })
  },
  searchBox:function(e){
    var that=this;
    
    console.log(e.detail.value.phone)
    if (that.data.yanzhengma_bool==true){
      request.post(
        constans.sendCode,
        { phone: e.detail.value.phone },
        function (data) {
          console.log(data)
          if (data.data.code == 0) {
            that.setData({
              yanzhengma: 60,
              yanzhengma_bool:false,
              phoneT: e.detail.value.phone,
                optBinds: 1,
            })
            var num = 60;
           
            function time1() {
              that.setData({
                yanzhengma: num
              })
              num = num - 1;
              if (num == 0) {
                clearInterval(time)
                that.setData({
                  yanzhengma: '重新发送验证码',
                  yanzhengma_bool: true
                })
              
              }
            }
           var time= setInterval(time1,1000)
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: data.data.message,
            })
          }
        })
    }
   

  },
  getPhoneNumber: function (e) {
    console.log(e)
    let that = this
  
   


   

        wx.login({
          success: res => {
            request.get(
              constans.minlogin,
              { code: res.code },
              function (data1) {
                console.log(data1)
                if(data1.data.code==0){
                
                  console.log(wx.getStorageSync('Authorization'), data1.data.data.session)
                  app.requestHeader = { 'Authorization': wx.getStorageSync('Authorization'), 'session': data1.data.data.session }
                  request.post(
                    constans.bindPhone,
                    { iv: e.detail.iv, encryptedData: e.detail.encryptedData },
                    function (data) {
                      console.log(data)
                      if(data.data.code==0){
                        app.requestHeader = { 'Authorization': wx.getStorageSync('Authorization') }
                        wx.showModal({
                          title: '提示',
                          showCancel: false,
                          content: data.data.message,
                          success: function () {
                            wx.navigateBack({
                              delta: 1
                            })
                          }
                        })
                      } else {
                        wx.showModal({
                          title: '提示',
                          showCancel: false,
                          content: data.data.message,
                        })
                      }
                    })
                } else {
                  wx.showModal({
                    title: '提示',
                    showCancel: false,
                    content: data.data.message,
                  })
                }
                
          })
          }
        })
      
    },
    // 验证码提交绑定
    bind_Ph:function(e){
      let that = this;
      
      if (parseFloat(that.data.optBinds)==1){
        request.post(
          constans.bindUserByCode,
          { phone: that.data.phoneT, token: e.detail.value.phoneT },
          function (data) {
            console.log(data)
            if (data.data.code == 0) {

              wx.showModal({
                title: '提示',
                showCancel: false,
                content: data.data.message,
                success: function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            } else {
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: data.data.message,
              })
              return;
            }

          })
      }else{

      }
      
     
   
    },
    bgBind:function(){
      let that = this;
      that.setData({
        optBind: 0,
        optBind_bg:0,
        dipBind: 'none',
      })
    },
    bindShow:function(){
      
        let that = this;
        that.setData({
          optBind: 1,
          optBind_bg: '.6',
          dipBind: 'block',
        })
      
      
    },
    onShow:function(){
      let that=this;
      request.get(
        constans.getProfile,
        {},
        function (data) {
          if(data.data.code==0){
            console.log(data)
            if (data.data.data.phone == '未绑定') {
              that.setData({
                phoneBool: 'none',
                phoneBool1: 'block',
              })
            } else {
              that.setData({
                phoneBool: 'block',
                phoneBool1: 'none',
                phOne: data.data.data.phone
              })
            }
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: data.data.message,
            })
            return;
          }
         
        })
    }
})
